package com.ngovanhau.justsort.model;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.entity.IEntity;
import org.anddev.andengine.entity.modifier.MoveModifier;
import org.anddev.andengine.entity.modifier.ParallelEntityModifier;
import org.anddev.andengine.entity.modifier.ScaleModifier;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.text.ChangeableText;
import org.anddev.andengine.entity.text.Text;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.util.modifier.IModifier;
import org.anddev.andengine.util.modifier.IModifier.IModifierListener;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;

import com.ngovanhau.justsort.MainGameActivity;
import com.ngovanhau.justsort.MySprite;
import com.ngovanhau.justsort.control.ValueControl;
import com.ngovanhau.justsort.item.config.MyConfig;

public class Bonus extends MySprite {

	private int coin = 0;
	Font font;
	BitmapTextureAtlas font_BTA;
	float size_font = 45;

	Text mText;

	public void onLoadResources(Engine engine, Context context) {
		this.engine = engine;
		this.context = context;
		this.mainGame = (MainGameActivity) context;

		this.font_BTA = new BitmapTextureAtlas(1024, 512,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.font = new Font(font_BTA, Typeface.createFromAsset(
				context.getAssets(), "font/TCCEB.TTF"), size_font
				* MyConfig.RACE_HEIGHT, true, Color.WHITE);
		engine.getTextureManager().loadTexture(font_BTA);
		engine.getFontManager().loadFont(font);
	}

	public void add(int px_start, int py_start, String text) {
		if (ValueControl.isCompletedLevel == true)
			return;

		final ChangeableText levelNext = new ChangeableText(
				(float) (px_start - Math.random() * 100),
				(float) (py_start - Math.random() * 100), font, text);
		levelNext.setScale(0f);
		levelNext.setColor(0.85f, 0.4f, 0.0f);
		scene.attachChild(levelNext);

		float time = 0.6f;
		final MoveModifier moveModifier = new MoveModifier(time,
				(float) (px_start - Math.random() * 100),
				(float) (px_start - Math.random() * 100),
				(float) (py_start - Math.random() * 100),
				(float) (py_start - Math.random() * 100));
		ScaleModifier mScaleModifier_in = new ScaleModifier(0.2f, 0.4f, 1.2f);

		ParallelEntityModifier mParallelEntityModifier_in = new ParallelEntityModifier(
				mScaleModifier_in, moveModifier);
		mParallelEntityModifier_in
				.addModifierListener(new IModifierListener<IEntity>() {

					@Override
					public void onModifierStarted(IModifier<IEntity> pModifier,
							IEntity pItem) {

					}

					@Override
					public void onModifierFinished(
							IModifier<IEntity> pModifier, IEntity pItem) {
						removeChangeableText(levelNext);
//						if (ValueControl.isCompletedLevel) {
//							mainGame.completedGame();
//						}
					}
				});

		levelNext.registerEntityModifier(mParallelEntityModifier_in);
	}

	public void removeChangeableText(final ChangeableText textLevelStart) {
		engine.runOnUpdateThread(new Runnable() {
			@Override
			public void run() {
				scene.detachChild(textLevelStart);
			}
		});
	}

	public int getCoin() {
		return coin;
	}

	public void onLoadScene(Scene mScene) {
		this.scene = mScene;
	}

	public void removeAnimatedSprite(final AnimatedSprite mAnimatedSprite) {
		engine.runOnUpdateThread(new Runnable() {
			@Override
			public void run() {
				scene.detachChild(mAnimatedSprite);
			}
		});
	}

	public void reset() {
		coin = 0;
		// if(ValueControl.TypeGame == ValueControl.Classic){
		// MyLog.println("reset updateProgressBarClassic");
		// Level.coinLevelCurrentClassic = this.coin;
		// mainGame.mProgressBar.updateProgressBarClassic();
		// }
		// else if(ValueControl.TypeGame == ValueControl.NewMode){
		// mText.setTextCoin(0);
		// mainGame.mProgressBar.updateProgressbarNewMode();
		// }
	}

	public void setBigCoin(Sprite bigcoin) {
		// this.x_end = (int) (bigcoin.getX() + (bigcoin.getWidth() / 2) -
		// (coin_TTR.getWidth() / 8) / 2);
		// this.y_end = (int) (bigcoin.getY() + (bigcoin.getHeight() / 2) -
		// (coin_TTR .getHeight() / 2));
	}

	public void setCoin(int coin) {
		this.coin = coin;
		// if(ValueControl.TypeGame == ValueControl.Classic){
		// Level.coinLevelCurrentClassic = this.coin;
		// mainGame.mProgressBar.updateProgressBarClassic();
		// }
	}

	public void setTextCoin(Text mText) {
		this.mText = mText;
	}

	public void upCoin() {
		if (!ValueControl.isCompletedLevel) {
			// if(ValueControl.TypeGame == ValueControl.Classic ||
			// ValueControl.TypeGame == ValueControl.TimeAttach){
			// coin = coin + 10;
			// mText.setTextCoin(coin);
			// if(ValueControl.TypeGame == ValueControl.Classic){
			// Level.coinLevelCurrentClassic = this.coin;
			// mainGame.mProgressBar.updateProgressBarClassic();
			// }
			// else if(ValueControl.TypeGame == ValueControl.NewMode){
			// Level.squareCurrent++;
			// mainGame.mProgressBar.updateProgressbarNewMode();
			// }
			mainGame.checkLevelCompleted();
			// }
		}
	}
}